package com.test;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;

public class FileMd5 {
    public static String getFileMD5(String filePath) throws Exception {

        MessageDigest md = MessageDigest.getInstance("MD5");
        Path path = Paths.get(filePath);
        byte[] fileBytes = Files.readAllBytes(path);
        byte[] digest = md.digest(fileBytes);
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        try {
            File folder = new File("/Users/xingyc/Downloads/TW8080-通用版-docs");
            if(folder.isDirectory()){
                File[] files = folder.listFiles();
                for(File file : files){
                    System.out.println("fileName: " + file.getName() + " md5: " + FileMd5.getFileMD5(file.getAbsolutePath()));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
